#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;

string Bucketsort(string s)
{
	vector<int> b(26,0);
	for(size_t i = 0; i<s.size();++i)
		++b[s[i]-'a'];
	s.clear();
	for(size_t i =0; i<b.size();++i)
	{
		for(size_t j = 0; j<b[i]; ++j)
			s.push_back('a'+i);
	}
	return s;
}


unsigned int Fib2(unsigned int n)
{
	unsigned int nRet, nP, nPp;
	nRet = nP = nPp = 1;

	if ((n == 1) || (n == 2))
		return nRet;

	for (unsigned int i = 3; i <= n; i++)
	{
		nRet = nP + nPp;

		nPp = nP;
		nP = nRet;
	}

	return nRet;
}


vector<string> Anagrams(vector<string>& strs)
{
	map<string,vector<size_t> > mapper;
	for(size_t i =0; i<strs.size(); ++i)
		mapper[Bucketsort(strs[i])].push_back(i);
	map<string,vector<size_t> >::iterator beg = mapper.begin();
	map<string,vector<size_t> >::iterator end = mapper.end();
	vector<string> result;
	while(beg!=end)
	{
		vector<size_t>& indics = beg->second;
		if(indics.size()>1)
		{
			for(size_t i=0; i<indics.size();++i)
				result.push_back(strs[indics[i]]);
		}
		++beg;
	}
	return result;
}



int main()
{
	string s[] = {"tea","and","ace","ad","eat","dans"};
	//string s("next");
	//cout<<Bucketsort(s)<<endl;
	vector<string> strs(&s[0],&s[5]);
	vector<string> ss = Anagrams(strs);
	return 0;
}